package com.cortex.papaya.adapter;

import java.util.List;

import com.cortex.papaya.search.Category;
import com.cortex.papaya.search.Link;
import com.cortex.papaya.search.Link.OnDataChangedListener;
import com.cortex.papaya.view.LinkView;
import com.cortex.papaya.view.LinkView.OnClickDocumentView;
import com.cortex.papayasearch.R;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.GridView;

public class DocumentAdapter extends BaseAdapter {

	private List<Link> documents;
	private Context context;
	private OnClickDocumentListener onClickDocumentListener;

	public DocumentAdapter(Context context,List<Link> documents){
		this.context = context;
		this.documents = documents;
	}
	
	@Override
	public int getCount() {
		return documents.size();
	}

	@Override
	public Object getItem(int position) {
		return documents.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	public void setOnClickDocumentListener(OnClickDocumentListener onClickDocumentListener) {
		this.onClickDocumentListener = onClickDocumentListener;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Link document = documents.get(position);
		if(convertView == null){
			LayoutInflater inflter = LayoutInflater.from(context);
			final LinkView documentView = (LinkView)inflter.inflate(R.layout.view_link, parent, false);
			document.setOnDataChangedListener(new OnDataChangedListener() {
				
				@Override
				public void dataChanged(Link document) {
					documentView.setDocument(document);
				}
			});
			documentView.setOnClickViewListener(new OnClickDocumentView() {
				
				@Override
				public void onClick(LinkView view) {
					if(onClickDocumentListener != null){
						onClickDocumentListener.documentClicked(documentView);
					}
				}
			});
			convertView = documentView;
		}
		((LinkView)convertView).setDocument(document);
		return convertView;
	}

	
	
	public interface OnClickDocumentListener{
		void documentClicked(LinkView documentView);
	}



	public void setDocuments(List<Link> documents) {
		this.documents = documents;
		notifyDataSetChanged();
	}
}

